<template>
<view class="project_tj_view">
	<ProjectItemCp
		v-for="(item,index) in dataList"
		:key="index"
		:itemData="item">
	</ProjectItemCp>
</view>
</template>

<script>
//项目推进页面
import ProjectItemCp from "@/components/public/ProjectItem.vue";
export default {
	name:"ProjectTjView",
	components:{ProjectItemCp},
	data() {
		return {
			pageList:[],  //每一页对应的数据(二维数组)
			page:0,  //已请求的页数
		};
	},
	computed:{
		dataList(){  //将二维pageList平铺
			let dataList = [];
			for(let item of this.pageList){
				dataList.push(...item);
			}
			return dataList;
		},
	},
	mounted(){
		this.getAllProject(1);  //默认获取第一页
	},
	onReachBottom(){  //滚动区域滚动到底部时获取下一页数据
		this.getAllProject(this.page+1);
	},
	methods:{
		getAllProject(page){
			uni.showLoading({
				title: '加载中'
			});
			this.$ajax({
				url: '/wx/project',
				method: 'GET',
				data:{
					page:page,
					size:10,
				},
				success: res => {
					let data = res.data.data;
					this.page = data.pages.page;
					data.pages.list.forEach(item =>{
						let nowDate = new Date();
						let totalDays = this.getDaysBetween(item.expectStartTime,item.expectEndTime);
						let nowDays = this.getDaysBetween(item.expectStartTime,nowDate);
						this.$set(item,'totalDays',totalDays);
						this.$set(item,'nowDays',nowDays);
					})
					this.$set(this.pageList,this.page-1,data.pages.list);  //索引必须是页数减一
					this.pageList.length = page;  //数组的长度必须和页数对应
				},
				complete: res =>{
					uni.hideLoading();
				}
			});
		},
		getDaysBetween(dateString1,dateString2){ // 获取两个日期之间的天数
		   var startDate = Date.parse(dateString1);
		   var endDate = Date.parse(dateString2);
		   var days=(endDate - startDate)/(1*24*60*60*1000);
		   return days;
		},
	},
}
</script>

<style lang="scss" scoped>
.project_tj_view{
	display: grid;
	grid-template-columns: 1fr;
}
</style>
